package fichier;

import java.awt.Container;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.*;


@SuppressWarnings("serial")
public class AjoutBox extends JDialog implements ActionListener{
	
	
	private String nomBox;
	private JPanel panelInfos;
	private JButton butEnregistrer;
	private JButton butAnnuler;
	private JLabel nomBoxLab;
	private JTextField nomBoxTxt;

	
	
	public AjoutBox(JFrame plop, Boolean plop2){
		super(plop, plop2);
		this.setBounds(200,200,350,150);
		this.initPanels();
		this.setTitre();
		
		this.setVisible(true);
	}
		
		
		
	public void initPanels( ){					
		Container contenu= this.getContentPane();
		contenu.setLayout(null);
						
		this.initPanelInfos();
					
		contenu.add(this.panelInfos);
		//contenu.add(this.panelButton) ;
					
		this.setBoundsPanels();
	
	}
	
		
	public void setBoundsPanels(){
		this.panelInfos.setBounds(0, 20, 300, 300);
		//this.panelButton.setBounds(65, 120, 200, 70);
		
	}	
	
	//x, y largeur, longueur
	
	public void initPanelInfos(){	
		this.panelInfos = new JPanel();
		this.nomBox = new String("");
		this.nomBoxLab = new JLabel("Nom du box");
		this.nomBoxTxt = new JTextField("");
		this.butEnregistrer = new JButton("Enregistrer");
		this.butAnnuler = new JButton("Annuler");
		
		this.panelInfos.add(this.butEnregistrer);
		this.panelInfos.add(this.butAnnuler);
		this.panelInfos.add(this.nomBoxLab);
		this.panelInfos.add(this.nomBoxTxt);	
		
		this.getContentPane().setLayout(null);
		this.panelInfos.setLayout(null);
		
		this.nomBoxLab.setBounds(60,20,80,20);
		this.nomBoxTxt.setBounds(160,20,120,20);
		this.butEnregistrer.setBounds(70,60,100,20);
		this.butAnnuler.setBounds(190,60,100,20);
		
		this.butEnregistrer.addActionListener(this);
		this.butAnnuler.addActionListener(this);	
		
		
	}
	
	public void setTitre() {
		this.setTitle("Ajout d'un Box");
	}

	public  void annule(){
		this.dispose();
	
	}
	


	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == this.butEnregistrer){
			
			if(nomBoxTxt.getText().equals("")){
				JOptionPane.showMessageDialog(this, "Vous n'avez rentré aucun nom de box", "Attention",1,new ImageIcon(getClass().getResource("/icon/warn.png")));
			}
			else{
				this.nomBox = this.nomBoxTxt.getText();
				@SuppressWarnings("unused")
				Box b = new Box(this.nomBox);
				if(!main.testUpdate)
				{
					JOptionPane.showMessageDialog(this, "Le box n'a pas pu être ajouté", "Attention",1,new ImageIcon(getClass().getResource("/icon/warn.png")));
				}
				else{
					JOptionPane.showMessageDialog(this, "Le box a bien été ajouté", "Attention",1,new ImageIcon(getClass().getResource("/icon/ok.png")));
					main.model.addElement(this.nomBoxTxt.getText());
					this.dispose();
				}
			}
		}
		
		if (e.getSource() == this.butAnnuler){
			int rep = JOptionPane.showConfirmDialog(this, "Etes-vous sûr de vouloir annuler ?", "Confirmation annulation",JOptionPane.YES_NO_OPTION);
			if (rep == JOptionPane.OK_OPTION)
				this.annule();
		}

	}
	
	public String getNomBox(){
		return this.nomBox;
	}
	
	public void setNomBox(String nomBox) {
		this.nomBox = nomBox;
	}
}	